import {
	IParam,
	ICalendarExpBarAbility,
	ICalendarExpBarController,
	ICalendarExpBarControllerParams,
	ICalendarExpBarStore,
	CalendarExpBarActionType,
	INavigateParam,
} from '@/core/interface';
import { ICalendarItemModel } from '@/core/interface/widgets/model/i-calendar-item-model';
import { computedNavData, deepCopy } from '@/core/utils';
import { ExpCtrlController } from './exp-ctrl-controller';

/**
 * 日历导航栏控制器
 *
 * @export
 * @class CalendarExpBarController
 * @extends {ExpCtrlController<CalendarExpBarActionType, ICalendarExpBarStore, ICalendarExpBarAbility>}
 * @implements {ICalendarExpBarController<ICalendarExpBarStore, ICalendarExpBarAbility>}
 */
export class CalendarExpBarController
	extends ExpCtrlController<
		CalendarExpBarActionType,
		ICalendarExpBarStore,
		ICalendarExpBarAbility
	>
	implements
		ICalendarExpBarController<ICalendarExpBarStore, ICalendarExpBarAbility>
{
	/**
	 * Creates an instance of CalendarExpBarController.
	 * @param {ICalendarExpBarControllerParams<CalendarExpBarActionType, ICalendarExpBarAbility>} params
	 * @memberof CalendarExpBarController
	 */
	public constructor(
		params: ICalendarExpBarControllerParams<
			CalendarExpBarActionType,
			ICalendarExpBarAbility
		>
	) {
		super(params);
		this.ctrlInit(params);
	}
	/**
	 * 日历导航栏模型
	 *
	 * @protected
	 * @param { ICalendarExpBarControllerParams<CalendarExpBarActionType, ICalendarExpBarAbility>} params
	 * @memberof CalendarExpBarController
	 */
	protected processCtrlInit(
		params: ICalendarExpBarControllerParams<
			CalendarExpBarActionType,
			ICalendarExpBarAbility
		>
	) {
		super.processCtrlInit(params);
		Object.assign(this.store, { selection: {} });
	}

	/**
	 * 处理选中数据变化
	 *
	 * @param {IParam[]} selections
	 * @memberof TreeExpBarController
	 */
	handleSelectionChange(selections: IParam[]) {
		if (!selections || selections.length === 0) {
			//  TODO 计算导航工具栏权限
			return;
		}
		const select = selections[0];
		const mdCtrl = this.getMDCtrlAbility();
		if (mdCtrl) {
			const itemType = select.itemType;
			const calendarModel = mdCtrl.getModel();
			const { calendarItems } = calendarModel;
			const calendarItem = calendarItems.find(
				(item: ICalendarItemModel) => itemType == item.itemType
			);
			if (calendarItem) {
				const { tempContext, tempViewParams } = this.computeNavParams(
					select,
					calendarItem
				);
				const param: INavigateParam[] = [
					{
						data: select,
						navViewName: calendarItem.navView,
						navData: {
							context: tempContext,
							viewParams: tempViewParams,
						},
					},
				];
				//  TODO 计算工具栏权限
				this.emit('selectionChange', param);
			}
		}
	}

	/**
	 * 计算导航参数
	 *
	 * @param {IParam} data
	 * @param {ICalendarModel} calendarItem
	 * @return {*}  {{ tempContext: any, tempViewParams: any }}
	 * @memberof TreeExpBarController
	 */
	public computeNavParams(
		data: IParam,
		calendarItem: ICalendarItemModel
	): { tempContext: any; tempViewParams: any } {
		const tempContext: any = {};
		const tempViewParams: any = {};
		const { context, viewParams } = this.store;
		if (context) {
			Object.assign(tempContext, deepCopy(context));
		}
		if (viewParams) {
			Object.assign(tempViewParams, deepCopy(viewParams));
		}
		// 主键
		Object.assign(tempContext, {
			[calendarItem.entityCodeName?.toLowerCase()]:
				data[calendarItem.entityKeyName?.toLowerCase()],
		});
		// 导航过滤参数
		if (calendarItem.navFilter) {
			Object.assign(tempViewParams, {
				[calendarItem.navFilter]:
					data[calendarItem.entityCodeName?.toLowerCase()],
			});
		}
		// 导航关系
		if (calendarItem.navPSDER?.minorCodeName) {
			Object.assign(tempViewParams, {
				['n_' + calendarItem.navPSDER.minorCodeName?.toLowerCase() + '_eq']:
					data[calendarItem.entityCodeName?.toLowerCase()],
			});
		}
		if (
			calendarItem.navigateContext &&
			Object.keys(calendarItem.navigateContext).length > 0
		) {
			const _context = computedNavData(
				data,
				tempContext,
				tempViewParams,
				calendarItem.navigateContext
			);
			Object.assign(tempContext, _context);
		}
		if (
			calendarItem.navigateParams &&
			Object.keys(calendarItem.navigateParams).length > 0
		) {
			const _params = computedNavData(
				data,
				tempContext,
				tempViewParams,
				calendarItem.navigateParams
			);
			Object.assign(tempViewParams, _params);
		}
		return { tempContext, tempViewParams };
	}
}
